home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 8596 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.2 KB  |  63 lines

  1. Newsgroups: comp.lang.c++
  2. Path: netcom.com!marnold
  3. From: marnold@netcom.com (Matt Arnold)
  4. Subject: Re: Am I imagining auto_ptr?
  5. Message-ID: <marnoldDMwwrJ.5Bn@netcom.com>
  6. Organization: NETCOM On-line Communication Services (408 261-4700 guest)
  7. References: <3124A4D5.FF@novell.com>
  8. Date: Sat, 17 Feb 1996 08:54:55 GMT
  9. Sender: marnold@netcom.netcom.com
  10.  
  11. Jeff Robison <jeffr@novell.com> writes:
  12.  
  13. >It seems like I recently read or heard or dreamed about a new keyword/type/whatever 
  14. >in the C++ draft standard called an auto_ptr.  I believe it will automatically 
  15. >delete its contents when it goes out of scope.  Something like this would be 
  16. >incredibly useful when handling (or rather not having to handle) exceptions.
  17.  
  18. >Am I imagining things, or does such a beast exist?  If it does exist, can you point 
  19. >me to some syntax/usage documentation?
  20.  
  21. It's nothing more than a template class.  It's not a special keyword or
  22. anything---you could easily create such a template yourself.  Many C++ 
  23. books on the market have covered the "automatic pointer" concept in one 
  24. form or another.  And, yes, the auto_ptr template class is part of the 
  25. C++ standard library.
  26.  
  27. Here's the *basic* idea...
  28.  
  29.    // this IS NOT the real auto_ptr class, it is only a simple
  30.    // illustration
  31.  
  32.    template <class T>
  33.    class auto_ptr
  34.       {
  35.       public:
  36.          auto_ptr(T* pt): m_pt(pt) 
  37.             { }
  38.  
  39.          ~auto_ptr() 
  40.             { delete m_pt; }
  41.         
  42.          T* operator->()           
  43.             { return m_pt; }
  44.  
  45.       private:
  46.          T* m_pt;
  47.       };
  48.  
  49.  
  50. Off the top of my head, I know that Scott Meyer's latest book "More
  51. Effective C++" lists the source code to the auto_ptr template class from
  52. the standard library.  It's not very complicated.  It's certainly not a
  53. "beast".
  54.  
  55. Regards,
  56. -------------------------------------------------------------------------
  57. Matt Arnold                       |        | ||| | |||| |  | | || ||
  58. marnold@netcom.com                |        | ||| | |||| |  | | || ||
  59. Boston, MA                        |      0 | ||| | |||| |  | | || ||
  60. 617.389.7384 (h) 617.576.2760 (w) |        | ||| | |||| |  | | || ||
  61. C++, MIDI, Win32/95 developer     |        | ||| 4 3 1   0 8 3 || ||
  62. -------------------------------------------------------------------------
  63.